//
//  MZYFineViewController.m
//  WristWatch
//
//  Created by dllo on 15/10/29.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "MZYFineViewController.h"
#import "MZYHeader.h"
#import "MZYinfoCollectionViewCell.h"
#import "MZYinfoConllectionViewController.h"


@interface MZYFineViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property(nonatomic, retain)UICollectionView *fineCollectionView;
@property(nonatomic, retain)NSMutableArray *fineArr;
@property(nonatomic, retain)MBProgressHUD *HUD;
@property(nonatomic, assign)NSInteger page;

@end

@implementation MZYFineViewController

- (void)dealloc {
    [_fineCollectionView release];
    [_fineArr release];
    [_HUD release];
    
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self createView];
    [self createData];
}


- (void)createData {
    // 解析fineCollectionView
    AFHTTPRequestOperationManager *conmanager = [AFHTTPRequestOperationManager manager];
    [conmanager GET:@"http://watch-cdn.idailywatch.com/api/list/video/zh-hans?page=1&ver=iphone&app_ver=10" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableArray *yArr = responseObject;
        self.fineArr = [NSMutableArray array];
        for (NSDictionary *tempDic in yArr) {
            MZYInformationModel *info = [[MZYInformationModel alloc] init];
            [info setValuesForKeysWithDictionary:tempDic];
           
            [self.fineArr addObject:info];
            [info release];
        }
        [self.fineArr removeLastObject];
        if (self.fineArr.count > 0) {
            self.HUD.hidden = YES;
            [self.fineCollectionView reloadData];
        }
        //        [self.conllectionArr removeObjectAtIndex:0];
        [self.fineCollectionView.header endRefreshing];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
}



- (void)createView {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(WIDTH / 2 - 15, HEIGHT / 3.5);
    flowLayout.minimumInteritemSpacing = 10;
    flowLayout.minimumLineSpacing = 10;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 10, 20, 10);
    
    self.fineCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) collectionViewLayout:flowLayout];
    [self.view addSubview:self.fineCollectionView];
    [flowLayout release];
    [self.fineCollectionView release];
    self.fineCollectionView.backgroundColor = [UIColor whiteColor];
    
    self.fineCollectionView.dataSource = self;
    self.fineCollectionView.delegate = self;
    
    [self.fineCollectionView registerClass:[MZYinfoCollectionViewCell class] forCellWithReuseIdentifier:@"reuser"];
    
    //下拉
    self.fineCollectionView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(head)];
    //上啦
    self.fineCollectionView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(foot)];
    
    self.page = 1;

}

- (void)head {
    
    [self createData];
    
}

- (void)foot {
    self.page++;

    NSString *curl = [NSString stringWithFormat:@"http://watch-cdn.idailywatch.com/api/list/video/zh-hans?page=%ld&ver=iphone&app_ver=10", self.page];
    // 解析CollectionView
    AFHTTPRequestOperationManager *conmanager = [AFHTTPRequestOperationManager manager];
    [conmanager GET:curl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableArray *yArr = responseObject;
        for (NSDictionary *tempDic in yArr) {
            MZYInformationModel *info = [[MZYInformationModel alloc] init];
            [info setValuesForKeysWithDictionary:tempDic];
            [self.fineArr addObject:info];
            [info release];
        }
         [self.fineArr removeLastObject];
        if (self.fineArr.count > 0) {
            self.HUD.hidden = YES;
            [self.fineCollectionView reloadData];
        }
        
        [self.fineCollectionView.footer endRefreshing];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.fineArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MZYinfoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuser" forIndexPath:indexPath];
    MZYInformationModel *info = [[MZYInformationModel alloc] init];
    
    info = self.fineArr[indexPath.row];
    cell.myLabel.text = info.title;
    if (indexPath.item == 0) {
        [cell.myImageView sd_setImageWithURL:[NSURL URLWithString:info.cover] placeholderImage:[UIImage imageNamed:@"3.jpg"]];
    } else{
        [cell.myImageView sd_setImageWithURL:[NSURL URLWithString:info.cover_thumb] placeholderImage:[UIImage imageNamed:@"3.jpg"]];
    }
    cell.layer.masksToBounds = YES;
    return cell;

}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"我被点击了11😠");
    MZYinfoConllectionViewController *conVC = [[MZYinfoConllectionViewController alloc] init];
    MZYInformationModel *info = self.fineArr[indexPath.row];
    conVC.hidesBottomBarWhenPushed = YES;
    conVC.link_wechat = info.link_wechat;
    [self.navigationController pushViewController:conVC animated:YES];
    [conVC release];

}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
